<?php
function __autoload($class_name) 
{
    include $class_name . '.php';
}
$page = new page('Sprava clanku');
$content = '';
if(isset($_SESSION['user']) && 
        $_SESSION['user']->validate() && 
                ($_SESSION['user']->getRights() >= 2))
{
    //TODO :sprava clanku
    $articles = new table(array(
    '<a href="'.$_SERVER['SCRIPT_NAME'].'?order_by=id">id</a>',
    '<a href="'.$_SERVER['SCRIPT_NAME'].'?order_by=auth_name">Autor</a>',
    '<a href="'.$_SERVER['SCRIPT_NAME'].'?order_by=sect_name">Sekce</a>', 
    '<a href="'.$_SERVER['SCRIPT_NAME'].'?order_by=date">Datum pridani</a>', 
    '<a href="'.$_SERVER['SCRIPT_NAME'].'?order_by=priority">Priorita</a>',
    '<a href="'.$_SERVER['SCRIPT_NAME'].'?order_by=header">Nadpis</a>',
    '<a href="'.$_SERVER['SCRIPT_NAME'].'?order_by=state">Stav</a>'));
    
    if(isset($_GET['page']))
    {
      $page_num = $_GET['page'];
    }
    else
    {
      $page_num = 1;
    }
    if(isset($_GET['order_by']))
            {
                $order_by = 'ORDER BY '.$_GET['order_by'].' ASC';
            }
            else
            {
                $order_by = '';
            }
    $query = 'SELECT COUNT(id) AS row_count FROM articles';
    $conn = new dbConnection();
    $sql_result = $conn->db_query($query);
    $row = mysql_fetch_array($sql_result,MYSQL_ASSOC);
    $row_count = (int)$row['row_count'];
    $query = 'SELECT articles.*,
        authors.name AS auth_name,
        sections.name AS sect_name
        
        FROM articles 
        INNER JOIN authors 
        ON articles.id_author = authors.id
        INNER JOIN sections 
        ON articles.id_section = sections.id
        '.$order_by.'
        LIMIT '.(($page_num-1)*5).', 5';
    $sql_result = $conn->db_query($query);
    while($row = mysql_fetch_array($sql_result,MYSQL_ASSOC))
    {  
        //array_push($row, "<a href=editArticle.php?id=$row[id]>zmenit</a>");
        ;
        $articles->addRow(array(
            $row['id'],
            $row['auth_name'],
            $row['sect_name'],
            $row['date'],
            $row['priority'],
            $row['header'],
            $row['state'],
            "<a href=editArticle.php?id=$row[id]>zmenit</a>"
            ));            
    }  
    $content .= $articles->render();
    $paging = new paging( $row_count, $page_num, 5);
    $content .= $paging->render();
        
}else
{
    $content .= 'Nemate dostatecna opravneni!';
    $content .= '<br />';        
}
echo $page->render($content);
?>
